package com.palm.hubo2o.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.palm.hubo2o.R;
import com.palm.hubo2o.event.BetEvent;
import com.palm.hubo2o.model.Bet;
import com.palm.hubo2o.util.EnumCode;
import com.palm.palmutils.MathUtils;
import com.palm.palmutils.widget.EasyRecyclerView;

import de.greenrobot.event.EventBus;


/**
 * Created by dest1 on 2015/9/22.
 * 双色球胆托
 */
public class SSQBankerFragment extends MostLotteryFragment {

    private static final int MAX_NUMBER_DANMA = 33;
    private static final int MAX_NUMBER_TUOMA = 33;
    private static final int MAX_NUMBER_BLUE = 16;
    private final Bet bet = new Bet(EnumCode.gameId.SSQ);


    @Override
    public void onCreateView2(View view, Bundle savedInstanceState) {
        super.onCreateView2(view, savedInstanceState);
        setupBalls("%02d", 1, MAX_NUMBER_DANMA, MAX_NUMBER_TUOMA, MAX_NUMBER_BLUE);
        bet.setPlayType(EnumCode.PlayType.SSQ_DT);
    }

    @Override
    protected void onSetMarkText(View parent, int groupIndex) {
        parent.findViewById(R.id.random_mark).setVisibility(View.GONE);
        TextView title = (TextView) parent.findViewById(R.id.title_mark);
        TextView tip = (TextView) parent.findViewById(R.id.tip_mark);
        if (groupIndex == 0) {
            title.setText("胆码");
            tip.setTextColor(red);
            tip.setText("至少选择1个,最多5个");
        } else if (groupIndex == 1) {
            title.setText("拖码");
            tip.setTextColor(red);
            tip.setText("至少选择2个,最多16个");
        } else {
            title.setText("蓝球");
            tip.setTextColor(blue);
            tip.setText("至少选择1个");
        }
    }

    @Override
    public boolean onRandomBalls() {
        return false;
    }

    @Override
    protected void onClearChoices() {
        super.onClearChoices();
        bet.getReds().clear();
        bet.getTuos().clear();
        bet.getBlues().clear();
    }

    @Override
    protected int onSetGroupColor(int groupIndex) {
        if (groupIndex == 0 || groupIndex == 1) {
            return red;
        } else {
            return blue;
        }

    }

    @Override
    protected boolean onBallChecked(EasyRecyclerView parent, boolean checked, int position, String ballText, int index) {
        if (index == 0) {//胆码区 最多5个
            if (checked) {
                if (bet.getReds().size() == 5) {
                    Toast.makeText(getContext(), "最多选择5个红球胆码", Toast.LENGTH_SHORT).show();
                    return false;
                }
                bet.getReds().add(ballText);
            } else {
                bet.getReds().remove(ballText);
            }
            if (checked && parent.isItemChecked(getAdapter().transToRawPosition(position, 1)))//胆码拖码互斥
                parent.setItemChecked(getAdapter().transToRawPosition(position, 1), false);
        } else if (index == 1) {//拖码区
            if (checked) {
                if (bet.getTuos().size() == 16) {
                    Toast.makeText(getContext(), "最多选择16个红球拖码", Toast.LENGTH_SHORT).show();
                    return false;
                }
                bet.getTuos().add(ballText);
            } else {
                bet.getTuos().remove(ballText);
            }
            if (checked && parent.isItemChecked(getAdapter().transToRawPosition(position, 0)))//胆码拖码互斥
                parent.setItemChecked(getAdapter().transToRawPosition(position, 0), false);
        } else {//蓝球区
            if (checked) {
                bet.getBlues().add(ballText);
            } else {
                bet.getBlues().remove(ballText);
            }
        }
        if (bet.getReds().size() + bet.getTuos().size() >= 7 && bet.getReds().size() >= 1 && bet.getTuos().size() >= 2 && bet.getBlues().size() >= 1) {
            bet.setWagerCount((int) (MathUtils.recursive(bet.getTuos().size(), 6 - bet.getReds()
                    .size()) / MathUtils.recursive(6 - bet.getReds().size()) * bet.getBlues().size()));//胆码差几位则从拖码里选择
            EventBus.getDefault().post(new BetEvent(bet));
        } else {
            EventBus.getDefault().post(new BetEvent());
        }
        return true;
    }


}
